package com.ruoyi.basice_info.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.basice_info.domain.TsExamSyllabusVo;
import com.ruoyi.basice_info.mapper.TsExamSyllabusVoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basice_info.mapper.TbTeacherMapper;
import com.ruoyi.basice_info.domain.TbTeacher;
import com.ruoyi.basice_info.service.ITbTeacherService;

/**
 * 教研组人员设置Service业务层处理
 *
 * @author hongyi
 * @date 2023-12-08
 */
@Service
public class TbTeacherServiceImpl implements ITbTeacherService
{
    @Autowired
    private TbTeacherMapper tbTeacherMapper;

    @Autowired
    private TsExamSyllabusVoMapper tsExamSyllabusVoMapper;
    /**
     * 查询教研组人员设置
     *
     * @param teacherId 教研组人员设置主键
     * @return 教研组人员设置
     */
    @Override
    public TbTeacher selectTbTeacherByTeacherId(Long teacherId)
    {
        return tbTeacherMapper.selectTbTeacherByTeacherId(teacherId);
    }

    @Override
    public TbTeacher getTbTeacherByTeacherCode(String code) {
        return tbTeacherMapper.getTbTeacherByTeacherCode(code);
    }

    /**
     * 查询教研组人员设置列表
     *
     * @param tbTeacher 教研组人员设置
     * @return 教研组人员设置
     */
    @Override
    public List<TbTeacher> selectTbTeacherList(TbTeacher tbTeacher)
    {
        return tbTeacherMapper.selectTbTeacherList(tbTeacher);
    }

    /**
     * 新增教研组人员设置
     *
     * @param tbTeacher 教研组人员设置
     * @return 结果
     */
    @Override
    public int insertTbTeacher(TbTeacher tbTeacher)
    {
        return tbTeacherMapper.insertTbTeacher(tbTeacher);
    }

    /**
     * 修改教研组人员设置
     *
     * @param tbTeacher 教研组人员设置
     * @return 结果
     */
    @Override
    public int updateTbTeacher(TbTeacher tbTeacher)
    {
        return tbTeacherMapper.updateTbTeacher(tbTeacher);
    }

    /**
     * 批量删除教研组人员设置
     *
     * @param teacherIds 需要删除的教研组人员设置主键
     * @return 结果
     */
    @Override
    public int deleteTbTeacherByTeacherIds(Long[] teacherIds)
    {
        int num = -1;
        int nums = 0;

        // 数组转为了集合
        List<Long> longList = Arrays.asList(teacherIds);
        for (Long item: longList) {
            TsExamSyllabusVo syllabusVo = new TsExamSyllabusVo();
            syllabusVo.setTeacherId(item);
            // 去子表查
            List<TsExamSyllabusVo> voList = tsExamSyllabusVoMapper.selectTsExamSyllabusList(syllabusVo);

            if (voList.size() <=0) {
                num  = tbTeacherMapper.deleteTbTeacherByTeacherId(item);
            } else {
                nums ++;
            }
        }
        if(longList.size() == 1) {
            if(nums > 0) {
                throw new IllegalArgumentException("该数据在考试大纲设置中使用，不能删除");
            } else {
                throw new IllegalArgumentException(nums + " 条数据删除失败，数据在考试大纲设置中使用，不能删除");
            }
        }
//        return tbTeacherMapper.deleteTbTeacherByTeacherIds(teacherIds);
        return num;
    }

    /**
     * 删除教研组人员设置信息
     *
     * @param teacherId 教研组人员设置主键
     * @return 结果
     */
    @Override
    public int deleteTbTeacherByTeacherId(Long teacherId)
    {
        return tbTeacherMapper.deleteTbTeacherByTeacherId(teacherId);
    }
}
